﻿using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ShopBack.Web.Controllers
{
    [Authorize]
    public class PrdouctController : Controller
    {
        // GET: Prdouct
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 商品数据
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public ActionResult GetPrdouct(int limit, int page, string ProductName, string Isstate)
        {
            var total = 0;
            var list = ProductService.GetProductMes(limit, page, ProductName, Isstate, out total).ToList();
            var list1 = list.Select(p => new { Id = p.Id, ProductName = p.ProductName, ProductImg = p.ProductImg ,Price = p.Price, Count = p.Count, Particulars = p.Particulars, CreateTime = p.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"), Site = p.Site, TypeId=p.TypeId, Isstate = p.Isstate });
            return Json(new { code = 0, msg = "", data = list1, count = total }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public ActionResult BatchDelArticele(string Ids)
        {
            int a = ProductService.GetBatchDelByIds(Ids);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "删除成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "删除失败" });
            }
        }
        /// <summary>
        /// 批量上架
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public ActionResult FaBu(string Ids)
        {
            int a = ProductService.FaBu(Ids);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "上架成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "上架失败" });
            }
        }
        /// <summary>
        /// 批量下架
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public ActionResult XiaJia(string Ids)
        {
            int a = ProductService.XiaJia(Ids);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "下架成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "下架失败" });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ArtDel(int id)
        {
            int a = ProductService.GetDel(id);
            if (a > 0)
            {
                return Json(new { Success1 = true, Message = "删除成功" });
            }
            else
            {
                return Json(new { Success1 = false, Message = "删除失败" });
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Add(Product prdouct)
        {
            int a = ProductService.GetAdd(prdouct);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "添加成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "添加失败" });
            }
        }
        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ArtEdit(int id)
        {
            var a = ProductService.GetProductId(id);
            return View(a);
        }
        /// <summary>
        /// 编辑数据
        /// </summary>
        /// <returns></returns>
        public ActionResult Update(Product prdouct)
        {
            int a = ProductService.GetUpdate(prdouct);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "修改成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "修改失败" });
            }
        }

    }
}